package com.atguigui.leetcode1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/6/6 11:19
 */
public class P2352EqualRowAndColumnPairs {
    public static void main(String[] args) {

        int[][] grid = {{3, 1, 2, 2}, {1, 4, 4, 5}, {2, 4, 2, 2}, {2, 4, 2, 2}};

        P2352EqualRowAndColumnPairs.Solution solution = new P2352EqualRowAndColumnPairs().new Solution();
        solution.equalPairs(grid);
        // TO TEST
    }


    class Solution {
        public int equalPairs(int[][] grid) {

            int ans = 0;
            Map<List<Integer>, Integer> map = new HashMap<>();

            int n = grid[0].length;
            for (int i = 0; i < n; i++) {
                List<Integer> arr = new ArrayList<Integer>();
                int[] ints = grid[i];
                for (int j = 0; j < n; j++) {
                    arr.add(ints[j]);
                }
                map.put(arr, map.getOrDefault(arr, 0) + 1);
            }

            for (int i = 0; i < n; i++) {
                List<Integer> arr = new ArrayList<Integer>();
                for (int j = 0; j < n; j++) {
                    arr.add(grid[j][i]);
                }
                if (map.containsKey(arr)) {
                    ans += map.get(arr);
                }

            }

            return ans;
        }

    }
}
